<template>
    <div>
        <el-card header="用户管理">
          <el-table :data="lists">
                <!-- 用户名 -->
                <el-table-column label="用户名" prop="username"></el-table-column>
                <!-- 密码 -->
                <el-table-column label="密码" prop="password"></el-table-column>
                <el-table-column label="操作">
                  <!-- 操作 -->
                    <template slot-scope="{row,column,$index}">
                        <el-button icon="el-icon-edit" @click.native="edit(row)" type="primary">编辑</el-button>
                        <el-button icon="el-icon-delete" @click.native="del($index,row.id)" type="danger">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>

        <!-- 对话框 begin -->
        <el-dialog title="编辑" :visible.sync="dialogVisible" width="30%">
            <el-form ref="form" :model="formData" :rules="rules">
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="formData.username"></el-input> 
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="formData.password"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="editSave">确 定</el-button>
            </span>
        </el-dialog>
        <!-- 对话框 end -->
    </div>
</template>
<script>
export default {
  data() {
    // 验证密码格式
      var validatePass = (rule, value, cb) => {
            var reg = /^(\w){6,20}$/;
            if (reg.test(value)) {
                  cb();
            } else {
                  cb(new Error("只能输入6-20个字母、数字、下划线"));
            }
      };
      // 验证用户名是否存在
      var usernameExist=(rule,value,cb)=>{
            this.$http.post('/users/usernameExist',this.$qs.stringify({username:value,oldvalue:this.formData.oldvalue})).then((result)=>{
                  if(result=='false'){
                        cb(new Error('用户名已存在'))
                  }else{
                  cb()
                  }
            })
      }
    return {
      lists: [],
      username:'',
      password:'',
      dialogVisible: false,
      formData: {
      username:'',
      password:'',
      id:'',
      oldvalue:'',
      },
      rules: {
        username: [
          { required: true, message: "用户名不能为空", trigger: "change" },
          { min: 6, max: 18, message: "长度在6~18位间", trigger: "change" },
          {validator:usernameExist, trigger: "change" },
        ],
        password:[
            { required: true, message: "密码不能为空", trigger: "change" },
            { validator: validatePass, trigger: "change" }
        ]
      }
    };
  },
  created() {
    // 获取所有用户信息请求
    this.$http.get("/users/list").then(result => {
      if (result.error == 0) {
        this.lists = result.data;
      }
    });
  },
  methods: {
    // 删除方法
    del(index, id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$http.post("/users/del",this.$qs.stringify({ id: id })).then(result => {
            if (result.error == 0) {
              this.lists.splice(index, 1);
              this.$message({
                type: "success",
                message: "删除成功"
              });
            } else {
              this.$message({
                type: "danger",
                message: "删除失败"
              });
            }
          });
      });
    },
    // 编辑方法
    edit(row) {
      this.dialogVisible = true;
      this.formData.username = row.username;
      this.formData.password = row.password;
      this.formData.id = row.id;
      this.formData.oldvalue=row.username
    },
    // 编辑保存
    editSave() {
      this.$refs.form.validate(pass => {
        if (pass) {
          this.dialogVisible = false;
          this.$http.post("/users/edit",this.$qs.stringify(this.formData)).then(result => {
              if (result.error == 0) {
                this.$http.get("/users/list").then(result => {
                  if (result.error == 0) {
                    this.lists = result.data;
                    this.$message({
                      type: "success",
                      message: "编辑成功"
                    });
                  }
                });
              }else{
                this.$message({
                  type:'danger',
                  message:'编辑失败'
                })
              }
            });
        }
      });
    }
  }
};
</script>

<style>
.el-button--primary {
  background: rgb(74, 151, 228);
}
</style>

